package com.zlsy.es.dianping.service.impl;

import com.baidu.unbiz.easymapper.MapperFactory;
import com.zlsy.es.dianping.baseexception.BizException;
import com.zlsy.es.dianping.pojo.dto.ShopDTO;
import com.zlsy.es.dianping.pojo.model.Category;
import com.zlsy.es.dianping.pojo.model.Seller;
import com.zlsy.es.dianping.pojo.model.Shop;
import com.zlsy.es.dianping.pojo.vo.response.ShopResponse;
import com.zlsy.es.dianping.service.CategoryService;
import com.zlsy.es.dianping.service.SellerService;
import com.zlsy.es.dianping.service.ShopService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * @author zhouliang
 * @date 2020/3/1 14:57
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShopServiceImpl implements ShopService {

    private final com.zlsy.es.dianping.mapper.ShopMapper shopMapper;
    private final CategoryService categoryService;
    private final SellerService sellerService;

    @Override
    public Shop create(Shop shopModel) {
        shopModel.setCreatedAt(new Date());
        shopModel.setUpdatedAt(new Date());

        //校验商家是否存在正确
        Seller seller = sellerService.get(shopModel.getSellerId());
        if (seller == null) {
            throw new BizException("商户不存在");
        }

        if (seller.getDisabledFlag().equals(1)) {
            throw new BizException("商户已禁用");
        }

        //校验类目
        Category category = categoryService.get(shopModel.getCategoryId());
        if (category == null) {
            throw new BizException("类目不存在");
        }
        shopMapper.insertSelective(shopModel);

        return get(shopModel.getId());
    }

    @Override
    public Shop get(Integer id) {
        Shop shopModel = shopMapper.selectByPrimaryKey(id);
        if (shopModel == null) {
            return null;
        }
        shopModel.setSellerModel(sellerService.get(shopModel.getSellerId()));
        shopModel.setCategoryModel(categoryService.get(shopModel.getCategoryId()));
        return shopModel;
    }

    @Override
    public List<Shop> selectAll() {
        List<Shop> shopModelList = shopMapper.selectAll();
        shopModelList.forEach(shopModel -> {
            shopModel.setSellerModel(sellerService.get(shopModel.getSellerId()));
            shopModel.setCategoryModel(categoryService.get(shopModel.getCategoryId()));
        });
        return shopModelList;
    }

    @Override
    public List<ShopResponse> recommend(BigDecimal longitude, BigDecimal latitude) {
        List<ShopDTO> shopModelList = shopMapper.recommendDTO(longitude, latitude);
        return this.buildTheSameList(shopModelList);
    }

    @Override
    public List<Map<String, Object>> searchGroupByTags(String keyword, Integer categoryId, String tags) {
        return shopMapper.searchGroupByTags(keyword, categoryId, tags);
    }

    @Override
    public Integer countAllShop() {
        return shopMapper.countAllShop();
    }

    @Override
    public List<ShopResponse> search(BigDecimal longitude,
                                     BigDecimal latitude, String keyword, Integer orderby,
                                     Integer categoryId, String tags) {
        List<ShopDTO> shopDTOS = shopMapper.searchDTO(longitude, latitude, keyword, orderby, categoryId, tags);
        return this.buildTheSameList(shopDTOS);
    }

    private List<ShopResponse> buildTheSameList(List<ShopDTO> shopDTOS) {
        MapperFactory.getCopyByRefMapper().mapClass(ShopDTO.class, ShopResponse.class).register();
        List<ShopResponse> list = shopDTOS.stream().map(shopDTO -> {
            ShopResponse shopResponse = MapperFactory.getCopyByRefMapper().map(shopDTO, ShopResponse.class);
            shopResponse.setSellerModel(sellerService.get(shopDTO.getSellerId()));
            shopResponse.setCategoryModel(categoryService.get(shopDTO.getCategoryId()));
            return shopResponse;
        }).collect(Collectors.toList());
        return list;
    }
}
